package org.lanqiao.dao.impl;

import org.lanqiao.dao.UserDao;
import org.lanqiao.entity.User;
import org.lanqiao.util.DBUtil;

import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public int addUser(User user) {
        String sql = "insert into user(username, password, name, address, birthday) values(?,?,?,?,?)";
        return DBUtil.executeUpdate(sql, user.getUsername(), user.getPassword(), user.getName(),
                user.getAddress(), user.getBirthday());
    }

    @Override
    public int updUser(User user) {
        String sql = "update user set username=?, password=?, name=?, address=?, birthday=? where id=?";
        return DBUtil.executeUpdate(sql, user.getUsername(), user.getPassword(), user.getName(),
                user.getAddress(), user.getBirthday(), user.getId());
    }

    @Override
    public int delUser(Integer id) {
        String sql = "delete from user where id=?";
        return DBUtil.executeUpdate(sql, id);
    }

    @Override
    public List<User> findAll() {
        String sql = "select * from user";
        return DBUtil.executeQuery(User.class, sql);
    }

    @Override
    public User findById(Integer id) {
        String sql = "select * from user where id=?";
        List<User> userList = DBUtil.executeQuery(User.class, sql, id);
        return userList.isEmpty() ? null : userList.get(0);
    }

    @Override
    public boolean isExistById(Integer id) {
        User user = this.findById(id);
        return user == null ? false : true;
    }
}
